module game {
    class PlayerHandle {
        public constructor() {
        }
        //创建角色
        public createPlayer(id) {
            var playerConfig = templateConfig.character[id];
            console.log(playerConfig)
            var player = new Player(playerConfig);
            player.bag = new Package({ //冒险背包
                type: 1,
                size: 10
            })

            player.bag.addItem(new ItemComponent({
                id: 200001,
                stack: 100
            }))

            player.bag.addItem(new ItemComponent({
                id: 200002,
                stack: 100
            }))

            player.bag.addItem(new ItemComponent({
                id: 130001,
                stack: 1
            }))

            player.bag.addItem(new ItemComponent({
                id: 200101,
                stack: 10
            }))

            player.bag.addItem(new ItemComponent({
                id: 200102,
                stack: 10
            }))

            player.bag.addItem(new ItemComponent({
                id: 200201,
                stack: 10
            }))
              
            player.bag.addItem(new ItemComponent({
                id: 200202,
                stack: 10
            }))
                
            player.bag.addItem(new ItemComponent({
                id: 200203,
                stack: 10
            }))
                
            player.store = new Package({ //仓库
                type: 2,
                size: 50
            })

            var pet = new Pet({
                id: 110001,
                lvl: 1
            })

            player.battlePets.push(pet)
            this.petBattle(player,0)
            var pet = new Pet({
                id: 110002,
                lvl: 1
            })
            player.battlePets.push(pet)
            this.petBattle(player,1)
            var pet = new Pet({
                id: 110003,
                lvl: 1
            })
            player.petHouse.push(pet)
            player.equipments.weapon = new Equipment({ //武器
                id: 250001
            })
            player.equipments.body = new Equipment({ //衣服
                id: 250003
            })
            player.equipments.head = new Equipment({ //帽子
                id: 250004
            })
            player.equipments.foot = new Equipment({ //鞋子
                id: 250005
            })
            player.equipments.decoration = new Equipment({ //饰品
                id: 250002
            })        
            player.initPlayer()
            return player
        }

        //装备升级
        public equipmentLvlupByPart(player: Player,part: string) {
            var id = player.equipments[part].id
            var lvl = player.equipments[part].lvl
            var equipmentConfig = templateConfig.equipment[id]
            var strengthenId = equipmentConfig.strengthenId
            var config = templateConfig.strengthen[strengthenId][lvl]
            var ret = utilsHandle.removeItemsAndGold(player,config)
            if(ret === 0) {
                player.equipments[part].lvl += 1
            }
            return
        }
        
        //装备升级
        public equipmentLvlup(player: Player,equipment) {
            var id = equipment.id
            var lvl = equipment.lvl
            var equipmentConfig = templateConfig.equipment[id]
            var strengthenId = equipmentConfig.strengthenId
            var config = templateConfig.strengthen[strengthenId][lvl]
            var ret = utilsHandle.removeItemsAndGold(player,config)
            if(ret === 0) {
                equipment.lvl += 1
            }
            return
        }
        
        //穿上装备
        public equip(player: Player, packageType: number, index) {
            var pack : Package
            if(packageType === gplayer.bag.type){
                pack = gplayer.bag
            } else if(packageType === gplayer.store.type){
                pack = gplayer.store
            }else{
                return
            }
            var equipment = pack.items[index]
            var equipmentConfig = totalConfig[equipment.id]
            
                var replace = player.equipments[equipmentConfig.part]
            
            player.equipments[equipmentConfig.part] = equipment
            if(replace){
                pack.items[index] = replace
            }else {
                pack.removeItemByPos(index)
            }
            
        }
        
        //装备卸下
        public unloadEquipment(player: Player,part: string) {
            var equipment = player.equipments[part]
            var ret = player.store.addItem(equipment)
            if(ret === -1) {
                ret = player.bag.addItem(equipment)
                if(ret === -1) {
                    return
                }

            }
            player.equipments[part] = null
        }
        
        //宠物替换位置
        public petTransPos(player: Player,pos: number,toPos: number) {
            var temp = player.battlePets[pos]
            player.battlePets[pos] = player.battlePets[toPos]
            player.battlePets[toPos] = temp
            player.calcAttri()
        }
        //宠物出战
        public petBattle(player: Player,index: number) {
            var pet: Pet = player.petHouse[index]
            if(!pet && player.battlePets.length < 5) {
                return
            }
            player.petHouse.splice(index,1)
            player.battlePets.push(pet)
            player.calcAttri()
            return 0
        }
        //宠物休息
        public petRest(player: Player,index: number) {
            var pet: Pet = player.battlePets[index]
            if(!pet) {
                return
            }
            player.battlePets.splice(index,1)
            player.petHouse.push(pet)
            player.calcAttri()
            return 0
        }

        //宠物升级
        public lvlup(pet) {
            pet.lvl += 1
        }

        //时间
        public timeRun() {
            gplayer.basicInfo.time += 1
            var seasonIndex = Math.floor(gplayer.basicInfo.time / 96 / 30 % 4)
            gplayer.season = seasonIndex
            if(gplayer.weather.flashTime <= gplayer.basicInfo.time) {
                gplayer.weather.index = Math.floor(Math.random() * 6) //['晴天','下雨','阴天','雷雨','下雪','沙暴']
                gplayer.weather.flashTime = gplayer.basicInfo.time + Math.floor(Math.random() * 6 + 6) * 4
            }          
            var timeRunEvent: TimeRunEvent = new TimeRunEvent(TimeRunEvent.name)
            timeRunEd.dispatchEvent(timeRunEvent)
        }
        
        //接受任务
        public acceptTask(taskId){
            //判断用户是否已经接了这个任务
            var tasks = gplayer.tasks
            for(var i in tasks){
                if(tasks[i].taskId === taskId){
                    return -1
                }
            }
            //判断任务是否已经完成过
            tasks = gplayer.completeTasks
            for(var i in tasks) {
                if(tasks[i] === taskId) {
                    return -1
                }
            }
            var task = new game.Task({
                taskId: taskId
            })
            gplayer.tasks.push(task)
            return 0
        }
        
        //任务状态 1没接 2接受 3完成
        public taskState(taskId) {
            //判断用户是否已经接了这个任务
            var tasks = gplayer.tasks
            for(var i in tasks) {
                if(tasks[i].taskId === taskId) {
                    return 2
                }
            }
            //判断任务是否已经完成过
            tasks = gplayer.completeTasks
            for(var i in tasks) {
                if(tasks[i] === taskId) {
                    return 3
                }
            }
            return 1
        }
        
        //格子任务状态 1没接 2没完成 3完成
        public taskStateByLattice(taskId, latticeId) {
            //判断用户是否已经接了这个任务
            var tasks = gplayer.tasks
            for(var i in tasks) {
                if(tasks[i].taskId === taskId) {
                    for(var j in tasks[i].com){
                        if(tasks[i].com === latticeId){
                            return 3
                        }
                    }
                    return 2
                }
            }          
            return 1
        }
        
        //完成任务
        public compeleteTask(taskId){
            var taskConfig = totalConfig[taskId]
            
            var tasks = gplayer.tasks
            for(var i in tasks) {
                if(tasks[i].taskId === taskId) {
                    var ret = this.taskIsCompelete(taskId)
                    if(ret === 2){
                        return -1
                    }
                    tasks.splice(1, 1)
                    gplayer.completeTasks.push(taskId)
                    if(taskConfig.nextTaskId){ //自动获取下个任务
                        var task = new game.Task({
                            taskId: taskConfig.nextTaskId
                        })
                        gplayer.tasks.push(task)
                    }
                    return 0
                }
            }
            return -1
        }
        
        //任务是否将完成 1可完成 2 还不能完成 3 任务不存在
        public taskIsCompelete(taskId){
            var taskConfig = totalConfig[taskId]
            var tasks = gplayer.tasks
            for(var i in tasks){
                if(tasks[i].taskId === taskId) { 
                    if(tasks[i].com.length === taskConfig.length){
                        return 1 
                    }else {
                        return 2
                    }
                }
            }
            return 3
        }
        //判断背包空间是否充足
        public packageIfEnough(items) {
            var length = items.length
            for(var i in items) {
                var itemId = items[i][0]
                var itemConfig = totalConfig[itemId]
                if(itemConfig.maxStack !== 1) {
                    var itemComponent = gplayer.bag.getItemComponent(itemId)
                    if(itemComponent) {
                        length -= 1
                    }
                }
            }

            if(gplayer.bag.emptySize >= length) {
                return 0
            } else {
                return -1
            }
        }
        
        //添加物品到背包
        public addItemsByArray(items) {
            var length = items.length
            for(var i in items) {
                var itemId = items[i][0]
                var num = items[i][1]
                var itemComponent = game.utilsHandle.createItem({
                    id:itemId,
                    stack:num
                })             
                gplayer.bag.addItem(itemComponent)                        
            }
        }
    }

    export var playerHandle = new PlayerHandle()
        
    export class TimeRunEvent extends egret.Event {
        public static name: string = "timeRun"
        public constructor(type: string,bubbles: boolean = false,cancelable: boolean = false) {
            super(type,bubbles,cancelable)
        }
    }
}